package org.mbds.wolf.seql;

import org.mbds.wolf.seql.exceptions.MalformedConfigFileException;

/**
 * 
 * @author alesas
 *
 */
public class  SeqlWords 
{
	public final static String EXEC 			= "EXEC";
	public final static String SELECT 			= "SELECT";
	public final static String INSERT 			= "INSERT";	
	public final static String DELETE 			= "DELETE"; 
	public final static String UPDATE 			= "UPDATE"; 
	public final static String ALTER 			= "ALTER"; 
	public final static String CREATE 			= "CREATE"; 
	public final static String DROP 			= "DROP";
	public final static String DISCOVER 		= "DISCOVER";
	public final static String GRANT 			= "GRANT";
	public final static String REVOKE 			= "REVOKE"; 
	public final static String CHECK 			= "CHECK"; 
	public final static String AUTH 			= "AUTH"; 
	public final static String BEGIN 			= "BEGIN";
	public final static String COMMIT 			= "COMMIT"; 
	public final static String ROLLBACK 		= "ROLLBACK";
	
	public final static String STR_INTO 		= "into";
	public final static String STR_FROM 		= "from";
	public final static String STR_SET 			= "set";
	public final static String STR_FETCH 		= "fetch";
	public final static String STR_VALUES 		= "values";
	public final static String STR_PARAMS 		= "params";
	public final static String STR_BRACKET_L	= "(";
	public final static String STR_BRACKET_R	= ")";
	public final static String STR_COMMA		= ",";
	public final static String STR_SEPARATOR	= ";";
	public final static String STR_QUOTE		= "'";
	
//	public final static String[] STR_FETCH 		= {"current", "first", "next", "last", "all" };
	
	public final static String STR_APPLET 		= "applet";
	public final static String STR_FIELD 		= "field";
	public final static String STR_COMMAND 		= "command";
	
	/**
	 * nextWord
	 * @param sentence
	 * @param word
	 * @return word
	 */
	public static String nextWord(final String sentence, final String word) 
	{
		if (sentence == null || sentence.length() == 0 || word == null)
		{
			return null;
		}
		int index = -1;
		if (word.equals(""))
		{
			//Get the first word
			index = sentence.indexOf(" ");
			String first = sentence.substring(0, index);
			return first;
		} 
		else
		{
			index = sentence.toLowerCase().indexOf(word.toLowerCase());
		}
		if (index==-1) 
		{
			return null;
		}
		int idx1 = sentence.indexOf(" ", index + word.length() + 1);
		idx1 = idx1 != -1 ? idx1 : sentence.length();
		return sentence.substring(index + word.length() + 1, idx1);
	}
	
	/**
	 * wordBetween
	 * @param sentence
	 * @param first
	 * @param second
	 * @return word
	 */
	public static String wordBetween(final String sentence, final String first, final String second) 
	{
		if (sentence == null || sentence.length() == 0 || first == null || second == null)
		{
			return null;
		}
		int indexFirst = sentence.indexOf(first);
		if (indexFirst==-1)
		{
			return null;
		}
		int indexSecond = sentence.indexOf(second, indexFirst+1);
		if (indexSecond==-1)
		{
			return null;
		}
		return sentence.substring(first.length() + indexFirst, indexSecond).trim();
	}

	/**
	 * checkRequiredWords
	 * @param sentence
	 * @param words
	 * @return boolean
	 * @throws MalformedConfigFileException
	 */
	public static void checkRequiredWords(final String sentence, final String... words) throws Exception 
	{
		if (sentence == null)
		{
			throw new MalformedConfigFileException();
		}
		for (String word : words) 
		{
			int index = sentence.toLowerCase().indexOf(word.toLowerCase());
			if (index == -1)
			{
				throw new MalformedConfigFileException();
			}
		}
	}
}
